// src/services/trainingService.js
// 实训记录管理接口

import api from './api';

/**
 * 新增实训记录
 * @param {Object} data - 实训记录数据，格式如下：
 * {
 *   name: string,         // 实训名称，必填
 *   course: string,       // 课程名称，必填
 *   type: string,         // 类型（线上/线下），必填
 *   status: string,       // 状态（进行中/已结束/即将开始），必填
 *   start: string,        // 开始时间，格式：2025-06-27T09:00:00.000+08:00，必填
 *   end: string,          // 结束时间，格式同上，必填
 *   registrations: number,// 报名人数，选填
 *   manager: string       // 负责人，必填
 * }
 * @returns {Promise<{success:boolean, message:string}>} true/false
 */
export async function addTrainingRecord(data) {
  console.log('addTrainingRecord', data);
  const res = await api.post('/api/training/add', data);
  return res.data === true
    ? { success: true, message: '新增成功' }
    : { success: false, message: '新增失败' };
}

/**
 * 修改实训记录
 * @param {Object} data - 实训记录数据，包含id，格式同addTrainingRecord
 * @returns {Promise<{success:boolean, message:string}>} true/false
 */
export async function updateTrainingRecord(data) {
  console.log('updateTrainingRecord', data);
  const res = await api.put('/api/training/update', data);
  return res.data === true
    ? { success: true, message: '修改成功' }
    : { success: false, message: '修改失败' };
}

/**
 * 删除实训记录
 * @param {number} id - 实训ID
 * @returns {Promise<{success:boolean, message:string}>} true/false
 */
export async function deleteTrainingRecord(id) {
  console.log('deleteTrainingRecord', id);
  const res = await api.delete(`/api/training/delete/${id}`);
  return res.data === true
    ? { success: true, message: '删除成功' }
    : { success: false, message: '删除失败' };
}

/**
 * 按名称模糊查询
 * @param {string} name - 实训名称
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function searchByName(name) {
  console.log('searchByName', name);
  const res = await api.get('/api/training/searchByName', { params: { name } });
  return Array.isArray(res.data) ? res.data : [];
}

/**
 * 按课程筛选
 * @param {string} course - 课程名称
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function searchByCourse(course) {
  console.log('searchByCourse', course);
  const res = await api.get('/api/training/searchByCourse', { params: { course } });
  return Array.isArray(res.data) ? res.data : [];
}

/**
 * 按类型筛选
 * @param {string} type - 类型
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function searchByType(type) {
  console.log('searchByType', type);
  const res = await api.get('/api/training/searchByType', { params: { type } });
  return Array.isArray(res.data) ? res.data : [];
}

/**
 * 按状态筛选
 * @param {string} status - 状态
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function searchByStatus(status) {
  console.log('searchByStatus', status);
  const res = await api.get('/api/training/searchByStatus', { params: { status } });
  return Array.isArray(res.data) ? res.data : [];
}

/**
 * 按负责人筛选
 * @param {string} manager - 负责人
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function searchByManager(manager) {
  console.log('searchByManager', manager);
  const res = await api.get('/api/training/searchByManager', { params: { manager } });
  return Array.isArray(res.data) ? res.data : [];
}

/**
 * 查询所有实训
 * @returns {Promise<Array<Object>>} Training[]
 */
export async function getAllTraining() {
  console.log('getAllTraining');
  const res = await api.get('/api/training/all');
  return Array.isArray(res.data) ? res.data : [];
}
